<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>图片</title>
</head>

<body>
    <div class="flex-center">
        <div class="img-box">
            <img crossorigin="anonymous" src="./assets/img/img1.jpeg" />
        </div>
        <div class="img-box">
            <img crossorigin="anonymous" src="./assets/img/img2.jpeg" />
        </div>
        <div class="img-box">
            <img crossorigin="anonymous" src="./assets/img/img3.jpeg" />
        </div>
        <div class="img-box">
            <img crossorigin="anonymous" src="./assets/img/img4.jpeg" />
        </div>
    </div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/color-thief/2.3.0/color-thief.umd.js"></script>
<script>
    const colorThief = new ColorThief();
    const img = document.querySelectorAll('img');
    const html = document.documentElement
    const length = img.length;

    function getColors() {
        for (let i = 0; i < length; i++) {

            img[i].addEventListener('mouseenter', function () {
                let colors = colorThief.getPalette(img[i], 3)
                let [c1, c2, c3] = colors.map(item => {
                    return `rgb(${item[0]},${item[1]},${item[2]})`;
                })
                html.style.setProperty('--c1', c1)
                html.style.setProperty('--c2', c2)
                html.style.setProperty('--c3', c3)
                img[i].classList.add('active')
                addOtherOpacity(i)
            })
            img[i].addEventListener('mouseleave', function () {
                html.style.setProperty('--c1', 'transparent')
                html.style.setProperty('--c2', 'transparent')
                html.style.setProperty('--c3', 'transparent')
                img[i].classList.remove('active')
                removeOtherOpacity(i)
            })
        }
    }
    getColors()
    function addOtherOpacity(i) {
        img.forEach((item,index)=>{
            if(index!=i){
                item.classList.add('opacity')
            }
        })
    }
    function removeOtherOpacity(i){
        img.forEach((item,index)=>{
            if(index!=i){
                item.classList.remove('opacity')
            }
        })
    }
</script>
<style>
    * {
        margin: 0;
        padding: 0;
        background: linear-gradient(var(--c1), var(--c2), var(--c3)) no-repeat;
    }

    .flex-center {
        width: 100%;
        height: 100vh;
        padding-top: 100px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }

    .img-box {
        width: 600px;
        height: 300px;
        margin-bottom: 100px;
    }

    .img-box img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: .6s;
    }

    .active {
        transform: scale(1.2);
        border: 5px solid #fff;
        border-radius: 5px;
        box-shadow: x-shadow 0 10px 10px rgba(0, 0, 0, .8);

    }

    .opacity {
        opacity: 0.2;
    }
</style>

</html>